An analysis of the difference between [] static arrays and dynamic arrays allocated by new in C++

  • 2020-04-02 02:36:03
  • OfStack

This article analyzes the difference between [] static array and new dynamic array in C++ language with examples, which can help you deepen your understanding of C++ language array. The specific differences are as follows:

When sizeof is performed on the static array name, the result is the sizeof the space occupied by the entire array;
So you can use sizeof(array name)/sizeof(* array name) to get the length of the array.
Int a, [5]. Sizeof (a)=20,sizeof(*a)=4. Since the entire array takes 20 bytes, the first element (int) takes 4 bytes.
Int * a = new int [4]; Then sizeof(a)=sizeof(*a)=4, because the address bit is 4 bytes, and int is 4 bytes.

2. When the static array is used as the function parameter, sizeof operation is performed on the array name in the function, and the result is 4, because The pointer that the array name represents is an address , which takes up 4 bytes of memory (because the compiler doesn't check the length of the array when passing arguments to the array name, see the previous article (link: #)). For the function name of the dynamic array, whenever I do sizeof, I get 4.

3. New also requires you to delete, which allocates space in the heap and is inefficient; And [] directly on the stack allocation, will automatically release, high efficiency, but the stack space is limited.

Return an array by function

The static array declared by the function cannot be returned by the function, because the memory used by the function to call its internal variables is freed due to lifetime problems. If you want to return an array through a function, you can create the array dynamically with new in the function and then return its first address.
The reason can be understood as the [] static array is applied in the stack, the local variables in the function are also in the stack, and the new dynamic array is allocated in the heap, so when the function returns, things in the stack are automatically released, and things in the heap will not be automatically released if there is no delete.

Here are some examples:


int *test(int *b) //B can be the name of a static array or the first address of a dynamic array
{
  for(int i=0;i<5;i++) //Outputs the elements of the incoming array
   cout<<*(b+i)<<" ";
  cout<<endl;
  int *c=new int[5]; //Dynamically create an array
  //If I change the green part to int c[5]; The c array cannot be obtained by calling test in the main function
  for(i=0;i<5;i++)  //The values of the new array are equal to the values of the array passed in plus 5
   *(c+i)=*(b+i)+5;
  return c;     //Returns the first address of the newly created dynamic array
}
int main()
{
 int *b=new int[5]; //Create a dynamic array b
 for(int i=0;i<5;i++)//The assignment
  *(b+i)=i; 
 //The green part can also be changed to int b[5]={0,1,2,3,4}; It can also be a static array
 int *c=test(b);   //Call the test function as an argument and assign the return value to c
 for(i=0;i<5;i++)  //Output the items of the array returned by test
   cout<<*(c+i)<<" ";
 cout<<endl;
 return 0;
}

I believe that after reading the case analysis of this article can further deepen the reader's understanding of C++ array.


Related articles: